<template>
  <el-dialog :visible="dialog" :title="title" :destroy-on-close="true" @close="cancel">
    <el-form ref="form" :model="form" :rules="rules">
      <el-form-item label="部门名称" :label-width="formLabelWidth" prop="name">
        <el-input v-model="form.name" style="width:80%" autocomplete="off" placeholder="1-50个字符" />
      </el-form-item>

      <el-form-item label="部门编码" :label-width="formLabelWidth" prop="code">
        <el-input v-model="form.code" style="width:80%" autocomplete="off" placeholder="1-50个字符" />
      </el-form-item>

      <el-form-item label="部门负责人" :label-width="formLabelWidth" prop="manager">
        <el-select v-model="form.manager" style="width:80%" placeholder="请选择" @focus="getDepartmentUser">
          <el-option
            v-for="item in people"
            :key="item.value"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="部门介绍" :label-width="formLabelWidth" prop="introduce">
        <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>

    <el-row type="flex" justify="center">
      <el-button type="primary" @click="add">确定</el-button>
      <el-button @click="cancel">取 消</el-button>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments, getDepartmentUser, addDepartment, getDepartmentDetail, changeDepartment } from '@/api/departments'
export default {
  props: {
    dialog: {
      type: Boolean,
      default: false
    },
    data: {
      required: true,
      type: Object
    }
  },
  data() {
    const validateDeptsName = (rule, value, callback) => {
      getDepartments().then(data => {
        console.log(data)
        const { depts } = data
        console.log(depts)
        depts.some(item => item.name === value && item.pid === this.data.id) ? callback(new Error('不能重复部门名字')) : callback()
      })
    }
    const validateDeptsCode = (rule, value, callback) => {
      console.log(1)
      getDepartments().then(data => {
        console.log(data)
        const { depts } = data
        console.log(depts)
        if (this.form.id) {
          depts.some(item => item.code === value && item.code !== this.form.code) ? callback(new Error('不能重复部门编码')) : callback()
        } else {
          depts.some(item => item.code === value) ? callback(new Error('不能重复部门编码')) : callback()
        }
      })
    }
    return {
      form: {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      },
      formLabelWidth: '120px',
      rules: {
        name: [
          { required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: validateDeptsName }
        ],
        code: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '输入1-50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: validateDeptsCode }
        ],
        manager: [
          { required: true, message: '不能为空', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '不能为空', trigger: 'blur' },
          { min: 1, max: 300, message: '输入1-300个字符', trigger: 'blur' }
        ]
      },
      people: {}
    }
  },
  computed: {
    title() {
      return this.form.id ? '编辑部门' : '新增部门'
    }
  },
  methods: {
    // 获取部门详情
    async getDepartmentDetail(id) {
      this.form = await getDepartmentDetail(id)
    },
    getDepartmentUser() {
      getDepartmentUser().then(res => {
        this.people = res
      })
    },
    async add() {
      try {
        console.log(1)
        const isOk = await this.$refs.form.validate()
        if (isOk) {
          // console.log(this.form)
          const data = {
            ...this.form, pid: this.data.id
          }
          if (this.form.id) {
            // console.log(this.form)
            // console.log(this.data)
            await changeDepartment(this.form.id, this.form)
          } else {
            await addDepartment(data)
          }
          this.$emit('closeDialog')
          this.$refs.form.resetFields()
        }
      } catch (error) {
        console.log(error)
      }
    },
    cancel() {
      this.form = {
        name: '',
        code: '',
        manager: '',
        introduce: ''
      }
      this.$emit('cancel')
    }

  }
}
</script>
<style lnag="scss"  scoped>
 /deep/ .el-dialog__header{
  background-color: #66b1ff;
}
/deep/ .el-dialog__close{
  color: #fff;
}
</style>
